﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace secureFTP_Client
{
    public partial class Form1 : Form
    {
        public static Form1 pForm1 = null;
        ClientConfig clientConfig;
        public Form1()
        {
            pForm1 = this;
            InitializeComponent();
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            clientConfig = new ClientConfig();
            clientConfig.SetPocketBufferLength(2*1024*1024);
            clientConfig.SetServerRootDirectory("C:\\Users\\slanyer\\Desktop");
            clientConfig.SetClientCorrentDirectory("C:\\Users\\slanyer");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Socket ConnectServerSocket;
            Socket ConnectServerDataSocket;
            FTPUser user = new FTPUser();
            user.User = textBox2.Text;
            user.PasswordSHA256 = textBox3.Text;
            try
            {
                //连接服务端控制端口配置
                clientConfig.SetServerIP(IPAddress.Parse(textBox1.Text));
                clientConfig.SetServerPort(Int32.Parse(textBox4.Text));
                ConnectServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress ServerIP = clientConfig.GetServerIP();
                Int32 ServerPort = clientConfig.GetServerPort();
                IPEndPoint ServerPoint = new IPEndPoint(ServerIP, ServerPort);
                
                //连接服务端控制端口
                ConnectServerSocket.Connect(ServerPoint);
                Console.WriteLine("连接服务端控制连接成功！" + ConnectServerSocket.RemoteEndPoint.ToString());


                //连接服务端数据端口配置
                clientConfig.SetServerDataPort(Int32.Parse(textBox5.Text));
                ConnectServerDataSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                Int32 ServerDataPort = clientConfig.GetServerDataPort();
                IPEndPoint ServerDataPoint = new IPEndPoint(ServerIP, ServerDataPort);

                //连接服务端数据端口
                ConnectServerDataSocket.Connect(ServerDataPoint);
                Console.WriteLine("连接服务端数据连接成功！" + ConnectServerDataSocket.RemoteEndPoint.ToString());


            }
            catch
            {
                MessageBox.Show("连接服务端失败！", "连接失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //连接成功后跳转到新窗口
            try
            {
                Form form2 = new Form2(ConnectServerSocket, ConnectServerDataSocket, user, this.clientConfig);
                form2.Show();
                this.Hide();
            }
            catch
            {

            }

        }
    }
}
